package com.ai.aiagent.demo.invoke;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.ContentType;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
public class HttpAiInvoke {

    public static void main(String[] args) {
        String apiKey = TestApiKey.API_KEY; // 替换为你的API Key
        String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation";

        // 构建 messages 数组
        JSONArray messages = new JSONArray();
        messages.add(new JSONObject()
                .set("role", "system")
                .set("content", "You are a helpful assistant."));
        messages.add(new JSONObject()
                .set("role", "user")
                .set("content", "明天佛山天气"));

        // 构建 input 对象
        JSONObject input = new JSONObject();
        input.set("messages", messages);

        // 构建 parameters 对象
        JSONObject parameters = new JSONObject();
        parameters.set("result_format", "message");

        // 构建最终请求体
        JSONObject body = new JSONObject();
        body.set("model", "qwen-plus");
        body.set("input", input);
        body.set("parameters", parameters);

        // 发送请求
        HttpResponse response = HttpRequest.post(url)
                .header("Authorization", "Bearer " + apiKey)
                .header("Content-Type", ContentType.JSON.getValue())
                .body(body.toString())
                .execute();

        System.out.println(response.body());
    }
}